package com.smarthome.web.property.controller.system;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smarthome.core.account.AccountService;
import com.smarthome.core.advicemanager.SystemMessageService;
import com.smarthome.core.user.PropertyUserService;
import com.smarthome.domain.bean.ShAccount;
import com.smarthome.domain.bean.ShAccountMessage;
import com.smarthome.domain.bean.ShUser;
import com.smarthome.domain.common.Result;
import com.smarthome.domain.enums.AccountMessageReceiveStatus;
import com.smarthome.domain.enums.AccountMessageSendStatus;
import com.smarthome.domain.enums.PushMobileType;
import com.smarthome.domain.vo.boss.Combobox;
import com.smarthome.domain.vo.boss.DataGrid;
import com.smarthome.domain.vo.boss.PageHelper;
import com.smarthome.domain.vo.boss.TimeRange;
import com.smarthome.rpc.PushMobileMessageService;
import com.smarthome.web.property.utils.UserHolder;

/**
 * 
* @ClassName: SystemMessageController
* @Description: 系统消息管理
* @author cyj
* @date 2015年9月24日 上午11:34:49
*
 */
@Controller
@RequestMapping("/systemMessageController")
public class SystemMessageController {
	
	@Resource
    private SystemMessageService systemMessageService;
	
	@Resource
	private AccountService accountService;
	
	@Resource
	private PropertyUserService propertyUserService;
	
	@Resource
	private PushMobileMessageService pushMobileMessageService;
	
	/**
	 * 
	* getPage(根据条件获取平台用户消息属性列表)
	* @Title: getPage
	* @Description: TODO
	* @param accountMessage
	* @param helper
	* @param request
	* @return
	* DataGrid
	* @throws
	* @author cyj
	* @date 2015年9月24日 下午12:06:04
	 */
	@RequestMapping("/dataGrid")
    @ResponseBody
	public DataGrid getPage(ShAccountMessage accountMessage,TimeRange timeRange,PageHelper helper){
		if(timeRange.getStartGmtCreate() != null) {
			accountMessage.addQueryData("startGmtCreate", timeRange.getStartGmtCreate());
		}
		if(timeRange.getEndGmtCreate() != null) {
			accountMessage.addQueryData("endGmtCreate", timeRange.getEndGmtCreate());
		}
		
		if(timeRange.getStartSendLastTime() != null) {
			accountMessage.addQueryData("startSendLastTime", timeRange.getStartSendLastTime());
		}
		if(timeRange.getEndSendLastTime() != null) {
			accountMessage.addQueryData("endSendLastTime", timeRange.getEndSendLastTime());
		}
		
		if(timeRange.getStartRecieveLastTime() != null) {
			accountMessage.addQueryData("startRecieveLastTime", timeRange.getStartRecieveLastTime());
		}
		if(timeRange.getEndRecieveLastTime() != null) {
			accountMessage.addQueryData("endRecieveLastTime", timeRange.getEndRecieveLastTime());
		}
		
		//获取登录的系统用户
    	ShUser shUser = UserHolder.getUser();
    	accountMessage.setSenderId(shUser.getId());
    	if(!UserHolder.isSuperResidence()) {
    		accountMessage.setResidenceId(UserHolder.getCurrentResidenceId());
    	}
		return systemMessageService.qeuryDataGrid(accountMessage, helper);
	}
	
	/**
	 * 
	* addPage(跳转添加平台用户消息页面)
	* @Title: addPage
	* @Description: TODO
	* @param request
	* @return
	* String
	* @throws
	* @author cyj
	* @date 2015年9月24日 下午12:05:44
	 */
	@RequestMapping("/addPage")
    public String addPage(HttpServletRequest request) {
        return "/system/adviceManager/systemMessageAdd";
	}
	
	
	/**
	 * 
	* add(添加平台用户消息)
	* @Title: add
	* @Description: TODO
	* @param accountMessage
	* @param recieveLoginName
	* @return
	* Result
	* @throws
	* @author cyj
	* @date 2015年9月24日 下午12:05:31
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Result add(ShAccountMessage accountMessage,String recieveLoginName) {
		if(StringUtils.isBlank(accountMessage.getTitle())) {
			return Result.failure("请输入标题！");
		}
		if(StringUtils.isBlank(accountMessage.getContent())) {
			return Result.failure("请输入内容！");
		}
    	ShUser shUser = UserHolder.getUser();
    	ShAccount account = new ShAccount();
		List<String> accountIdsList = new ArrayList<String>();
		if(StringUtils.isNotBlank(recieveLoginName)) {
			account.addQueryData("recieveLoginName",recieveLoginName);
		}
    	if(!UserHolder.isSuperResidence()) {
    		account.setResidenceId(UserHolder.getCurrentResidenceId());
    	}
    	accountIdsList = accountService.getShAccountIds(account);
		Date date = new Date();
		int accountTotal = accountIdsList.size();
		ShAccountMessage[] accountMessages = new ShAccountMessage[accountTotal];
		String[] accountIds = accountIdsList.toArray(new String[accountTotal]);
		
		ShAccountMessage newAccountMessage = null;
		for(int i = 0;i < accountTotal;i++) {
			newAccountMessage = new ShAccountMessage();
			BeanUtils.copyProperties(accountMessage, newAccountMessage);
			newAccountMessage.setSenderId(shUser.getId());
			newAccountMessage.setGmtCreate(date);
			newAccountMessage.setGmtModify(date);
			newAccountMessage.setSendLastTime(date);
			newAccountMessage.setSendStatus(AccountMessageSendStatus.SENT.getCode());
			newAccountMessage.setRecieveStatus(AccountMessageReceiveStatus.NOTVIEWED.getCode());
			newAccountMessage.setRecieverId(Long.parseLong(accountIds[i]));
			accountMessages[i] = newAccountMessage;
		}
		try{
			if(accountMessages.length > 0) {
				pushMobileMessageService.pushMsgToSome(accountMessage.getTitle(), PushMobileType.ACCOUNTMESSAGE.getCode(), accountIds);
		    	systemMessageService.add(accountMessages);
			}
		} catch(Exception e){
			e.printStackTrace();
			return Result.failure(e.getMessage());
			  
		}
		return Result.success("发送成功！");
	 }
	  
	
	  
	  
	  /**
	   * 
	  * delete(根据id删除平台用户消息)
	  * @Title: delete
	  * @Description: TODO
	  * @param id
	  * @return
	  * Result
	  * @throws
	  * @author cyj
	  * @date 2015年9月24日 下午12:05:04
	   */
	  @RequestMapping("/delete")
	  @ResponseBody
	  public Result delete(Long id) {
		  try{
			  systemMessageService.delete(id);
		  } catch(Exception e){
			  return Result.failure("公告被引用，不能删除");
		  }
		  return Result.success("删除成功！");
	  }
	  
	  /**
	   * 
	  * getComboData(获取平台用户消息管理所需的combo数据)
	  * @Title: getComboData
	  * @Description: TODO
	  * @return
	  * Map<String,Object>
	  * @throws
	  * @author cyj
	  * @date 2015年9月24日 下午12:04:20
	   */
	  @RequestMapping("/getComboData")
	  @ResponseBody
	  public Map<String, Object> getComboData() {
	  	  Map<String,Object> result = new HashMap<>();
    	  List<Combobox> sendStatus = systemMessageService.sendStatus();
    	  List<Combobox> recieveStatus = systemMessageService.recieveStatus();
    	  result.put("sendStatus", sendStatus);
    	  result.put("recieveStatus", recieveStatus);
    	  return result;
	  }
	
}
